package cn.lf.service.impl;

import cn.lf.constant.DelFlagTypeEnum;
import cn.lf.constant.ResponseCodeEnum;
import cn.lf.dto.carouseimg.*;
import cn.lf.entity.CarouselImg;
import cn.lf.entity.FileInfo;
import cn.lf.exception.SelfException;
import cn.lf.mapper.CarouselImgMapper;
import cn.lf.mapper.FileInfoMapper;
import cn.lf.service.ICarouselImgService;
import cn.lf.utils.MinioEnhance;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author linf
 * @since 2024-04-18
 */
@Service
public class CarouselImgServiceImpl extends ServiceImpl<CarouselImgMapper, CarouselImg> implements ICarouselImgService {

    @Resource
    private FileInfoMapper fileInfoMapper;

    @Override
    public IPage<AllCiRepDTO> getAllCarouselImgPage(AllCiReqDTO params) {

        Page<AllCiRepDTO> page = new Page<>(params.getCurrentPage(),params.getPageSize());

        return baseMapper.selectAllCarouselImgPage(page,params);
    }

    @Override
    public void saveCarouselImg(CiSaveReqDTO ciSaveReqDTO) {

        CarouselImg carouselImg = null;

        if (Objects.isNull(ciSaveReqDTO.getCarouselImgId())||Objects.equals(ciSaveReqDTO.getCarouselImgId(),"")){

            carouselImg = new CarouselImg();

            BeanUtils.copyProperties(ciSaveReqDTO,carouselImg);

            baseMapper.insert(carouselImg);

        }else {

            carouselImg = baseMapper.selectById(ciSaveReqDTO.getCarouselImgId());

            BeanUtils.copyProperties(ciSaveReqDTO,carouselImg);

            baseMapper.updateById(carouselImg);

        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delCarouselImg(String imgId) {

        CarouselImg carouselImg = baseMapper.selectById(imgId);

        baseMapper.deleteById(carouselImg);

        FileInfo fileInfo = fileInfoMapper.selectById(carouselImg.getFileId());

        if (Objects.isNull(fileInfo)){
            throw new SelfException(ResponseCodeEnum.SYS_ERROR.getCode(),"文件不存在");
        }

        fileInfo.setDelFalg(DelFlagTypeEnum.HAVING_DEL.getCode());

        fileInfoMapper.updateById(fileInfo);

        String targetPath = fileInfo.getFilePath()+fileInfo.getFileName();

        boolean delFlag = MinioEnhance.removeObject(targetPath);

        if (!delFlag){
            throw new SelfException(ResponseCodeEnum.SYS_ERROR.getCode(),"找不到对应文件");
        }

    }

    @Override
    public void setCarouselImgStatus(CiStatusSetReqDTO ciStatusSetReqDTO) {

        LambdaUpdateWrapper<CarouselImg> wrapper = new LambdaUpdateWrapper<>();

        wrapper.set(CarouselImg::getCarouselImgStatus,ciStatusSetReqDTO.getCarouselImgStatus());

        wrapper.eq(CarouselImg::getCarouselImgId,ciStatusSetReqDTO.getCarouselImgId());

        baseMapper.update(null,wrapper);


    }

    @Override
    public SingleCiInfoRepDTO getCarouselImgById(String imgId) {
        return baseMapper.selectCarouselImgById(imgId);
    }
}
